﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using CastleDefender.ConfigFolder;

namespace CastleDefender.Object.Monsters
{
    public partial class Monsters : DrawableGameComponent
    {
        #region VARIABLE

        protected List<Monster> lstMonsters;
        protected List<Texture2D> lstTextureMonsterStupids;
        protected List<Texture2D> lstTextureMonsterFlys;
        protected int intAmountMonsterStupid;
        protected int intAmountMonsterFly;
        protected int intAmount;
        protected Random radCreateMonsters;
        protected int intWidthFrames;
        protected int intHeightFrames;
        protected TimeSpan tsTimeCreateNewMonsterStupid;
        protected int intTimeSpanCreateNewMonsterStupid;
        protected TimeSpan tsTimeCreateNewMonsterFly;
        protected int intTimeSpanCreateNewMonsterFly;
        protected Vector2 vecTarget;

        #endregion

        #region CONSTRUCTOR

        public Monsters(Game myGame, ref List<Texture2D> textureMonsterStupids, ref List<Texture2D> textureMonsterFly,int widthFrames, int heightFrames,
            int amountMonsterStupid, int amountMonsterFly, int timeCreateNewMonsterStupid, int timeCreateNewMonsterFly,Vector2 target)
            : base(myGame)
        {
            lstMonsters = new List<Monster>();
            radCreateMonsters = new Random();
            lstTextureMonsterStupids = textureMonsterStupids;
            lstTextureMonsterFlys = textureMonsterFly;
            intAmountMonsterStupid = amountMonsterStupid;
            intAmountMonsterFly = amountMonsterFly;
            intWidthFrames = widthFrames;
            intHeightFrames = heightFrames;
            intTimeSpanCreateNewMonsterStupid = timeCreateNewMonsterStupid;
            intTimeSpanCreateNewMonsterFly = timeCreateNewMonsterFly;
            vecTarget = target;
            intAmount = intAmountMonsterStupid + intAmountMonsterFly;
        }
        #endregion

        #region PUBLIC METHOD

        public override void Initialize()
        {
            for (int i = 0; i < lstMonsters.Count; i++)
            {
                lstMonsters[i].Initialize();
            }

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            //Tạo 1 monster stupid sau 1 khoảng thời gian
            tsTimeCreateNewMonsterStupid += gameTime.ElapsedGameTime;
            if (tsTimeCreateNewMonsterStupid >= TimeSpan.FromSeconds(intTimeSpanCreateNewMonsterStupid))
            {
                tsTimeCreateNewMonsterStupid -=TimeSpan.FromSeconds(intTimeSpanCreateNewMonsterStupid);
                CreateNewMonsterStupid();
                intAmount = intAmountMonsterStupid + intAmountMonsterFly;
            }

            //Tạo 1 monster fly sau 1 khoảng thời gian
            tsTimeCreateNewMonsterFly += gameTime.ElapsedGameTime;
            if(tsTimeCreateNewMonsterFly >= TimeSpan.FromSeconds(intTimeSpanCreateNewMonsterFly))
            {
                tsTimeCreateNewMonsterFly -= TimeSpan.FromSeconds(intTimeSpanCreateNewMonsterFly);
                CreateNewMonsterFly();
                intAmount = intAmountMonsterStupid + intAmountMonsterFly;
            }

            for (int i = 0; i < lstMonsters.Count; i++)
            {
                lstMonsters[i].Update(gameTime);

                //Xóa quái bay khi ra khỏi màn hình
                if ((lstMonsters[i] is MonsterFly && (lstMonsters[i].Bounds.Y == Config.GameBounds.Height)) || lstMonsters[i].Life == 0)
                {
                    lstMonsters.RemoveAt(i);
                }
               
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            for (int i = 0; i < lstMonsters.Count; i++)
            {
                lstMonsters[i].Draw(gameTime);
            }

            base.Draw(gameTime);
        }
        #endregion

        #region PRIVATE METHOD

        //Tạo mới 1 monster stupid
        private void CreateNewMonsterStupid()
        {
            if (intAmountMonsterStupid != 0)
            {
                //Chọn ngẫu nhiên hướng 
                int state = radCreateMonsters.Next(100) % 2;
                
                //Thiết lập tọa độ xuất hiện ngẫu nhiên 
                Vector2 postion;
                if (state == 0)
                {
                    postion = new Vector2(-intWidthFrames, Config.GameSurFace - intHeightFrames);
                }
                else
                {
                    postion = new Vector2(Config.MapSize.X, Config.GameSurFace - intHeightFrames);
                }

                Texture2D ttMonster = lstTextureMonsterStupids[state];
                MonsterStupid newMonster = new MonsterStupid(Game, ref ttMonster, intWidthFrames, intHeightFrames,
                    Config.MonsterLifeMonsterStupid, state, Config.MonsterSpeedMonsterStupid, vecTarget, postion);

                
                lstMonsters.Add(newMonster);
                intAmountMonsterStupid--;
            }
        }

        //Tạo mới 1 monster fly
        private void CreateNewMonsterFly()
        {
            if (intAmountMonsterFly != 0)
            {
                //Tọa độ ngẫu nhiên 
                int randomPosX = radCreateMonsters.Next(Config.GameBounds.Width);

                //Tọa độ ngẫu nhiên nằm trong vùng an toàn trên
                if (randomPosX >= Config.GameBounds.Width / 2 && randomPosX < Config.GameBounds.Width / 2 + Config.MonsterFlyLimitPosXMax)
                {
                    randomPosX = Config.GameBounds.Width / 2 + Config.MonsterFlyLimitPosXMax;
                }

                //Tọa độ ngẫu nhiên nằm trong vùng an toàn dưới
                if (randomPosX < Config.GameBounds.Width / 2 && randomPosX > Config.GameBounds.Width / 2 - Config.MonsterFlyLimitPosXMax)
                {
                    randomPosX = Config.GameBounds.Width / 2 - Config.MonsterFlyLimitPosXMax;
                }

                Vector2 position = new Vector2(randomPosX, -intWidthFrames);

                //Xét hướng bay
                int state = position.X >= Config.GameBounds.Width / 2 ? 1 : 0;

                Texture2D ttMonster = lstTextureMonsterFlys[state];
                MonsterFly newMonster = new MonsterFly(Game, ref ttMonster, intWidthFrames, intHeightFrames,
                    Config.MonsterLifeMonsterFly, state, Config.MonsterSpeedMonsterFly, vecTarget, position);

                lstMonsters.Add(newMonster);
                intAmountMonsterFly--;
            }
        }

        #endregion

        #region PUBLIC PROPERTIES

        //Nhận trả giá trị lstMonsters
        public List<Monster> ListMonsters
        {
            get { return lstMonsters; }
            set { lstMonsters = value; }
        }

        //Trả giá trị intAmount
        public int Amount
        {
            get { return intAmount; }
            private set { }
        }
       
        #endregion

    }
}
